package com.xinghou.platform.dao.main.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

import com.xinghou.platform.dao.main.common.DBUserLevelCached;
import com.xinghou.platform.model.main.User;
import com.xinghou.platform.util.JsonUtil;
import com.xinghou.platform.util.StringUtils;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Aspect
@Component
public class UserCacheAspect extends DBUserLevelCached {
	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getUserById(..))")
	public void getUserById(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getUserByMobile(..))")
	public void getUserByMobile(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getUserBySecondAccount(..))")
	public void getUserBySecondAccount(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getUserBySecondAccountMap(..))")
	public void getUserBySecondAccountMap(){
	}


	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getUserByPassword(..))")
	public void getUserByPassword(){
	}


	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.updateUser(..))")
	public void updateUser(){
	}

	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.getCheckMobileCount(..))")
	public void getCheckMobileCount(){
	}
	@Pointcut("execution(public * com.xinghou.platform.dao.main.UserDAO.deletePwdCache(..))")
	public void deletePwdCache(){
	}


	@Around("getUserById()")
	public User getUserById(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_USERID , id);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			User user = (User)pjp.proceed();
			if(user != null)
				getRedisClient().set(key, JsonUtil.toJson(user), CACHED_DB_INDEX);
			return user;
		}else{
			User user = (User)JsonUtil.fromJson(jsonToken.toString(), User.class);
			return user;
		}
	}
	@Around("getUserByMobile()")
	public User getUserByMobile(ProceedingJoinPoint pjp) throws Throwable{
		String id = ((Object[])pjp.getArgs())[0].toString();
		String key = String.format(CACHE_USER_MOBILE , id);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			User user = (User)pjp.proceed();
			if(user != null)
				getRedisClient().set(key, JsonUtil.toJson(user), CACHED_DB_INDEX);
			return user;
		}else{
			User user = (User)JsonUtil.fromJson(jsonToken.toString(), User.class);
			return user;
		}
	}

	@Around("getUserBySecondAccount()")
	public User getUserBySecondAccount(ProceedingJoinPoint pjp) throws Throwable{
		String type = ((Object[])pjp.getArgs())[0].toString();
		String authId = ((Object[])pjp.getArgs())[1].toString();
		String key = String.format(CACHE_USER_SECOND,type,authId);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			User user = (User)pjp.proceed();
			if(user != null)
				getRedisClient().set(key, JsonUtil.toJson(user), CACHED_DB_INDEX);
			return user;
		}else{
			User user = (User)JsonUtil.fromJson(jsonToken.toString(), User.class);
			return user;
		}
	}
	@Around("getUserBySecondAccountMap()")
	public User getUserBySecondAccountMap(ProceedingJoinPoint pjp) throws Throwable{
		HashMap temp = (HashMap)((Object[])pjp.getArgs())[0];
		String type=temp.get("type").toString();
		String authId = temp.get("account").toString();
		String key = String.format(CACHE_USER_SECOND,type,authId);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			User user = (User)pjp.proceed();
			if(user != null)
				getRedisClient().set(key, JsonUtil.toJson(user), CACHED_DB_INDEX);
			return user;
		}else{
			User user = (User)JsonUtil.fromJson(jsonToken.toString(), User.class);
			return user;
		}
	}





	@Around("updateUser()")
	public int updateUser(ProceedingJoinPoint pjp) throws Throwable{
		
		User user = (User)((Object[])pjp.getArgs())[0];
		
		int returnno = (int) pjp.proceed();
		
		String keyuserid = String.format(CACHE_USERID , user.getUserid());
		String keymobile = String.format(CACHE_USER_MOBILE , user.getPhonenumber());
		
		if(getRedisClient().exists(keyuserid, CACHED_DB_INDEX)){
			getRedisClient().set(keyuserid, JsonUtil.toJson(user), CACHED_DB_INDEX);
		}
		
		if(getRedisClient().exists(keymobile, CACHED_DB_INDEX)){
			getRedisClient().set(keymobile, JsonUtil.toJson(user), CACHED_DB_INDEX);
		}
		
		if(StringUtils.isNotBlank(user.getQqid())){
			String authqqId = String.format(CACHE_USER_SECOND ,"QQ",user.getQqid());
			if(getRedisClient().exists(authqqId, CACHED_DB_INDEX)){
				getRedisClient().set(authqqId, JsonUtil.toJson(user), CACHED_DB_INDEX);
			}
		}
		
		if(StringUtils.isNotBlank(user.getWxid())){
			String authwxId = String.format(CACHE_USER_SECOND ,"WX",user.getWxid());
			if(getRedisClient().exists(authwxId, CACHED_DB_INDEX)){
				getRedisClient().set(authwxId, JsonUtil.toJson(user), CACHED_DB_INDEX);
			}
		}
		
		if(StringUtils.isNotBlank(user.getWbid())){
			String authwbId = String.format(CACHE_USER_SECOND ,"WB",user.getWbid());
			if(getRedisClient().exists(authwbId, CACHED_DB_INDEX)){
				getRedisClient().set(authwbId, JsonUtil.toJson(user), CACHED_DB_INDEX);
			}
		}

		if(StringUtils.isNotBlank(user.getPhonenumber())&&StringUtils.isNotBlank(user.getPassword())){
			String key = String.format(CACHE_USER_MOBILE_PASSWORD,user.getPhonenumber(),user.getPassword());
			getRedisClient().del(key, CACHED_DB_INDEX);
		}
		
		return returnno;
	}

	@Around("getCheckMobileCount()")
	public int getCheckMobileCount(ProceedingJoinPoint pjp) throws Throwable{
		String mobile = (String)((Object[])pjp.getArgs())[0];
        int count =0;
		String key = String.format(CACHE_CHECKMOBILE_COUNT,mobile);
		String countStr = getRedisClient().get(key, CACHED_DB_INDEX);
		if(StringUtils.isBlank(countStr)||"null".equals(countStr)){
			getRedisClient().setex(key,60*60*24,"1",CACHED_DB_INDEX);
		}else{
			count=Integer.parseInt(countStr);
			getRedisClient().incr(key,CACHED_DB_INDEX);
		}
		return count;
	}

	@Around("getUserByPassword()")
	public User getUserByPassword(ProceedingJoinPoint pjp) throws Throwable{
		String mobile = (String)((Object[])pjp.getArgs())[0];
		String password = (String)((Object[])pjp.getArgs())[1];
		String key = String.format(CACHE_USER_MOBILE_PASSWORD,mobile,password);
		Object jsonToken = getRedisClient().get(key, CACHED_DB_INDEX);
		if(jsonToken == null){
			User user = (User)pjp.proceed();
			if(user != null)
				getRedisClient().set(key, JsonUtil.toJson(user), CACHED_DB_INDEX);
			return user;
		}else{
			User user = (User)JsonUtil.fromJson(jsonToken.toString(), User.class);
			return user;
		}
	}

	@Around("deletePwdCache()")
	public void deletePwdCache(ProceedingJoinPoint pjp) throws Throwable{
		User user = (User)((Object[])pjp.getArgs())[0];
		String key = String.format(CACHE_USER_MOBILE_PASSWORD,user.getPhonenumber(),user.getPassword());
		getRedisClient().del(key, CACHED_DB_INDEX);

	}
}
